import {fetch} from '@/utils/fetch'
import {user, group, groupMember} from '../fragment'

export const getInfo = async ({ encryptedData, iv, groupOwnerId = 0 }) => {
  let res = await fetch({
    log: false,
    query: `mutation ($encryptedData: String!, $iv: String!, groupOwnerId: Int) {
      wxGroupInfo (encryptedData: $encryptedData, iv: $iv, groupOwnerId: $groupOwnerId) {
        id
        name
        avatarUrl
        openGroupId
        createdAt
      }
    }`,
    variables: {
      encryptedData,
      iv,
      groupOwnerId
    },
    cache: false
  })
  return res.data.wxGroupInfo
}

export const create = async ({ name }) => {
  let res = await fetch({
    query: `mutation ($name: String!) {
      groupCreate (name: $name) {
        ${group}
      }
    }`,
    variables: {
      name
    },
    cache: false
  })
  return res.data.groupCreate
}

export const getMembersById = async (groupId) => {
  let res = await fetch({
    query: `{
      group (id: ${groupId}) {
        members {
          ${groupMember}
        }
      }
    }`,
    cache: false
  })
  return res.data.group.members || []
}

export const removeMembersByIds = async (groupId, memberIds) => {
  let res = await fetch({
    query: `mutation($groupId: Int!, $memberIds: [Int!]!){
      groupRemoveMembersByIds (groupId: $groupId, memberIds: $memberIds)
    }`,
    variables: {
      groupId,
      memberIds
    },
    cache: false
  })
  return res.data.groupRemoveMembersByIds
}

export const getById = async ({groupId, cache = true}) => {
  let res = await fetch({
    query: `{
      group (id: ${groupId}) {
        ${group}
        owner {
          ${user}
        }
        members {
          ${groupMember}
        }
      }
    }`,
    cache
  })
  return res.data.group
}

export const getMyGroupInfo = async (groupId) => {
  let res = await fetch({
    query: `{
      groupMyGroupInfo (groupId: ${groupId}) {
        ${groupMember}
      }
    }`,
    cache: false
  })
  return res.data.groupMyGroupInfo
}

export const getMys = async () => {
  let res = await fetch({
    query: `{
      groupMys {
        ${group}
      }
    }`,
  })
  return res.data.groupMys
}

export const getMyRelates = async () => {
  let res = await fetch({
    query: `{
      groupMyRelates {
        ${group}
      }
    }`,
    cache: false
  })
  return res.data.groupMyRelates
}

export const join = async ({groupId, name, role}) => {
  let res = await fetch({
    query: `mutation ($groupId: Int!, $name: String!, $role: String!) {
      groupJoin (groupId: $groupId, name: $name, role: $role)
    }`,
    variables: {
      groupId,
      name,
      role
    },
  })
  return res.data.groupJoin
}

export const modifyMemberInfo = async ({groupId, name}) => {
  let res = await fetch({
    query: `mutation ($groupId: Int!, $name: String!) {
      groupModifyMemberInfo (groupId: $groupId, name: $name)
    }`,
    variables: {
      groupId,
      name
    }
  })
  return res.data.groupModifyMemberInfo
}

export const disband = async (groupId) => {
  let res = await fetch({
    query: `mutation ($groupId: Int!) {
      groupDisband (groupId: $groupId)
    }`,
    variables: {
      groupId
    }
  })
  return res.data.groupDisband
}

export const modifyInfo = async ({groupId, name}) => {
  let res = await fetch({
    query: `mutation ($groupId: Int!, $name: String!) {
      groupModify (groupId: $groupId, name: $name) {
        ${group}
      }
    }`,
    variables: {
      groupId,
      name
    }
  })
  return res.data.groupModify
}
